#coding=utf-8
'''
万维网访问对象
Created on 2009-3-22
@author: James
'''
import win32com.client, pythoncom
import time
class page:
    def __init__(self,url,visible=1):
        self.ie=win32com.client.DispatchEx('InternetExplorer.Application.1')
        self.ie.Visible = visible
        self.setUrl(url)        
        
    def setUrl(self,url):
        "设置页面地址"
        self.url=url
        
    def __navigate(self,url=None):
        "页面导航"
        if url is None:
            url=self.url
        elif url==self.url:
            return
        self.setUrl(url)          
        self.ie.Navigate(url)
        while self.ie.Busy:
              time.sleep(1)
           
    def getLinks(self,url=None):
        "获得所有链接，参数:url"  
        self.__navigate(url)        
        document = self.ie.Document
        links=document.links
        return links
    
    def getImages(self,url=None):
        "获得所有图像"
        self.__navigate(url)
        return self.ie.Document.images
    
    def close(self):
        "关闭连接"
        if self.ie is not None:
            self.ie.Quit()
    
